package com.aabte.lota.auth.auth.shiro;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
import org.apache.shiro.realm.Realm;

/**
 * @author Daniel
 * @version 1.0
 * @date 2020/4/5
 */
public class CustomerModularRealmAuthenticator extends ModularRealmAuthenticator {

  @Override
  protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken)
      throws AuthenticationException {
    List<Realm> supportRealm = new LinkedList<>();

    Collection<Realm> realms = getRealms();
    for (Realm realm : realms) {
      if (realm.supports(authenticationToken)) {
        supportRealm.add(realm);
      }
    }

    if (supportRealm.size() == 1) {
      return doSingleRealmAuthentication(supportRealm.get(0), authenticationToken);
    } else {
      return doMultiRealmAuthentication(supportRealm, authenticationToken);
    }
  }
}
